package com.meihua.code.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.IOException;
import java.net.InetAddress;
import java.util.Properties;

/**
 * @program: code
 * @description:
 * @author: meihua
 * @created: 2021/08/16 17:50
 */
public class PropertiesUtils {

    private final  static Logger logger= LoggerFactory.getLogger(PropertiesUtils.class);

    private  static Properties properties  ;

    static {
        try {
            properties =   PropertiesLoaderUtils.loadProperties( new ClassPathResource("application.properties"));
        } catch (IOException e) {
            logger.error("————读取配置文件 application.properties：出现异常，读取失败———— ",e);
        }
    }

    /**
     * 通过配置文件名读取内容
     * @param fileName
     * @return
     */
    public static Properties readPropertiesFile(String fileName) {
        try {
            Resource resource = new ClassPathResource(fileName);
            return  PropertiesLoaderUtils.loadProperties(resource);
        } catch (Exception e) {
            logger.error("————读取配置文件 application.properties：出现异常，读取失败———— ",e);
        }
        return null;
    }

    public static String getConfig(String name){
        return  properties.getProperty(name);
    }


    public static void main(String[] args)  {
        logger.info(getConfig("raft.cluster.config"));
    }
}
